using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Ada
{
	public sealed class Config
	{
		private Dictionary<string, string> configTokens;
		private string _serverListPath;
		private string _downloadPath;
        private bool _enableNamedPipes;
        private bool _enableTCP;
		
		private static readonly Config instance = new Config();
		
		//Force .net compiler to not apply beforefieldinit. That would be very naughty indeed.
		static Config()
		{
		}
		
		Config()
		{	
		}
		
		public string ServerListPath
		{
			get
			{
				return _serverListPath;
			}
		}
		
		public string DownloadPath
		{
			get
			{
				return _downloadPath;
			}
		}

        public bool UseNamedPipes
        {
            get
            {
                return _enableNamedPipes;
            }
        }

        public bool UseTCP
        {
            get
            {
                return _enableTCP;
            }
        }
		
		public void LoadConfig (string filePath)
		{
						configTokens = new Dictionary<string, string>();
			
			//If we're launching without arguments, use the default options, thanks.
			if(string.IsNullOrEmpty(filePath))
				filePath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "config.ini";

            System.IO.StreamReader configReader;
            try
            {
                configReader = new System.IO.StreamReader(Path.GetFullPath(filePath));
                while (!configReader.EndOfStream)
                {
                    string[] configLine = configReader.ReadLine().Split('=');
                    configTokens.Add(configLine[0], configLine[1]);
                }
                ProcessTokens();
            }
            catch (FileNotFoundException notFoundException)
            {
                Console.WriteLine("AdaNZB.Config: " + notFoundException.FileName + " was not found. Using defaults.");

                //http://stackoverflow.com/questions/1143706/getting-the-path-of-the-home-directory-in-c
                this._downloadPath = (Environment.OSVersion.Platform == PlatformID.Unix || 
                                        Environment.OSVersion.Platform == PlatformID.MacOSX)
                                        ? Environment.GetEnvironmentVariable("HOME")
                                        : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
                this._serverListPath = System.Environment.CurrentDirectory;
                this._enableNamedPipes = false;
                this._enableTCP = false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("AdaNZB.Config: " + ex.Message);
            }
			
			

			
		}
		
		private void ProcessTokens()
		{
			foreach(KeyValuePair<string, string> configItem in configTokens)
			{
				switch(configItem.Key.ToString().ToLower())
				{
					case "serverlist":
						_serverListPath = configItem.Value.ToString();
						break;
					case "downloadpath":
						_downloadPath = configItem.Value.ToString();
						break;
                    case "enablenamedpipes":
                        _enableNamedPipes = bool.Parse(configItem.Value);
                        break;
                    case "enabletcp":
                        _enableTCP = bool.Parse(configItem.Value);
                        break;
				    default:
					    Console.WriteLine("AdaNZB: Config: Unknown config item: " + configItem.ToString());
					    break;
				}
			}
		}
		
		public static Config Instance
		{
			get
			{
				return instance;
			}
		}
	}
}

